package com.zhf.dao;

import com.zhf.bean.Grade;
import com.zhf.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/24 17:29
 */
public class GradeDaoImp implements GradeDao {
    @Override
    public int addGrade(Grade grade) throws SQLException {
        Object classname = grade.getClassname();
        Object grade1 = grade.getGrade();
        Object adviser = grade.getAdviser();
        Object slogan = grade.getSlogan();
        int classsize = grade.getClasssize();
        Object[] param={classname,grade1,adviser,slogan,classsize};

        String sql="insert into grade (classname , grade , adviser , slogan , classsize) values (?,?,?,?,?)";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        int update = queryRunner.update(sql, param);
        return update;
    }

    @Override
    public int updateGrade(Grade grade) throws SQLException {
        Object classname = grade.getClassname();
        Object grade1 = grade.getGrade();
        Object adviser = grade.getAdviser();
        Object slogan = grade.getSlogan();
        int classsize = grade.getClasssize();
        Object[] param={grade1,adviser,slogan,classsize,classname};

        String sql="update grade set  grade=?, adviser=?, slogan=?, classsize=? where classname=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        int update = queryRunner.update(sql, param);
        return update;
    }

    @Override
    public int delGrade(String gradeName) throws SQLException {
        String sql="delete from grade where classname=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        int update = queryRunner.update(sql, gradeName);
        return update;
    }

    @Override
    public Grade getGrade(String gradeName) throws SQLException {
        String sql="select * from grade where classname=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Grade grade = queryRunner.query(sql, new BeanHandler<Grade>(Grade.class), gradeName);
        return grade;
    }
}
